/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package assignment_cp3120.user;

import assignment_cp3120.auth.Authenticate;
import assignment_cp3120.dataObjects.User;
import assignment_cp3120.user.deleteUser.DeleteUserView;
import assignment_cp3120.user.edituser.EditUserView;
import assignment_cp3120.startup.StartupView;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * View for the UCUser use case
 * @author Administrator
 */
public class UserView extends JPanel{
    private JButton deleteButton;
    private JPanel deletePanel;
    private JLabel deleteLabel;

    private JButton editButton;
    private JPanel editPanel;
    private JLabel editLabel;

    private JButton logoutButton;
    private JPanel logoutPanel;
    private JLabel logoutLabel;

    private JPanel previousContent;
    
    private static UserView instance;   
        
    /**
     * Defualt constructor
     */
    protected UserView() {
        previousContent = StartupView.getInstance().getContents();
        createPanel();
        StartupView.getInstance().setSubTitle("Welcome user: " + Authenticate.getInstance().getFirstname());
        instance = this;
    }
    
    public static UserView getInstance(){
        if(instance == null){
            instance = new UserView();
        }
        return instance;
    }
    private void createPanel(){        
        GridLayout layout = new GridLayout(0,1);
        FlowLayout fl = new FlowLayout();
        
        editButton = new JButton("Edit");
        editPanel = new JPanel(fl);
        editLabel = new JLabel("Delete this account");
        
        deleteLabel = new JLabel("Delete this account");
        deletePanel = new JPanel(fl);
        deleteButton = new JButton("Delete");
        
        logoutLabel = new JLabel("Logout of this account");
        logoutPanel = new JPanel(fl);
        logoutButton = new JButton("Logout");
        
        editPanel.add(editLabel);
        editPanel.add(editButton);
        
        deletePanel.add(deleteLabel);
        deletePanel.add(deleteButton);
        
        logoutPanel.add(logoutLabel);
        logoutPanel.add(logoutButton);
        
        add(editPanel);
        add(deletePanel);
        add(logoutPanel);
        
        setLayout(layout);

        deleteButton.addActionListener(new DeleteAL());
        editButton.addActionListener(new EditAL());
        logoutButton.addActionListener(new LogoutAL());
    }

    private class LogoutAL implements ActionListener{
        public void actionPerformed(ActionEvent e) {
           logout();
        }  
    }
    private class EditAL implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            new EditUserView().showGUI();
        }        
    }
    /**
     * Function to logout, displays the welcome screen and logs user out
     */
    public void logout(){
        StartupView.getInstance().setSubTitle("");
        StartupView.getInstance().clearContents();
        StartupView.getInstance().setContents(previousContent);
        Authenticate.setInstance(new User());
    }
    protected class DeleteAL implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            DeleteUserView.getInstance().showGUI();
        }        
    }    
}
